package com.nercel.dsj.gksales.service.impl;

import com.nercel.dsj.gksales.dao.mapper.ConfigurationMapper;
import com.nercel.dsj.gksales.model.entity.Configuration;
import com.nercel.dsj.gksales.service.ConfigurationService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ConfigurationServiceImpl implements ConfigurationService {

	private final ConfigurationMapper configurationMapper;

	private final RedisTemplate redisTemplate;

	public List<Configuration> list(String name, String type)  {
		return configurationMapper.getConfigurations(name,type);
	}

	public Configuration getConfigurationById(int id){
		return configurationMapper.getConfigurationById(id);
	}

	public Configuration getConfigurationByName(String name){
		return configurationMapper.getConfigurationByName(name);
	}

	public Configuration getConfigurationByLast(){
		return configurationMapper.getConfigurationByLast();
	}

	@Override
	public Map<String, Object> getConfigurationStringMap(Boolean updateRedisFlg) {
		Map<String, Object> result = new HashMap<>();
		Map<String,Configuration> map = getConfigurationMap(updateRedisFlg);
		map.forEach((name, conf)->{
			result.put(name, conf.getValue());
		});
		return result;
	}

	public void saveConfiguration(Configuration configuration){
		configurationMapper.saveConfiguration(configuration);
		this.getConfigurationMap(true);
	}

	public void updateConfiguration(Configuration configuration){
		configurationMapper.updateConfiguration(configuration);
		this.getConfigurationMap(true);
	}

	public void deleteConfiguration(int id){
		configurationMapper.deleteConfiguration(id);
		this.getConfigurationMap(true);
	}

	public Map<String,Configuration> getConfigurationMap(Boolean updateRedisFlg){
		List<Configuration> configurationList = (List<Configuration>)redisTemplate.opsForValue().get("configurations");
		if (updateRedisFlg || (configurationList == null || configurationList.size() == 0)){
			configurationList = (List<Configuration>)configurationMapper.getConfigurations(null,null);
			redisTemplate.opsForValue().set("configurations",configurationList,7200l,TimeUnit.SECONDS);
		}
		return configurationList.stream().collect(Collectors.toMap(Configuration::getName,(configuration -> configuration)));
	}

	@Override
	public String getAsString(String name) {
		Configuration configuration = configurationMapper.getConfigurationByName(name);
		return configuration.getValue();
	}

	@Override
	public Integer getAsInteger(String name) {
		Configuration configuration = configurationMapper.getConfigurationByName(name);
		if(configuration != null){
			return Integer.valueOf(configuration.getValue());
		}
		return null;
	}


}

